%%% FULL ANALYSIS SCRIPT

close all; clear all;

%% VARIABLES TO CHANGE

subject    = 'Subject 38';

global condition
condition  = 4;    % 1 = fast only (22 deg/s)
                   % 2 = slow only (18 deg/s)
                   % 3 = two speeds (22 and 18 deg/s)
                   % 4 = two speeds (old 2012 version)
                   
% MEG exp setup
scnw       = 1024;   % screen width in pixels
scnwcm     = 38.5;   % screen width in cm
view_dist  = 57.5;   % subject distance from screen in cm

% Behavioural exp setup
% scnw       = 1600; % screen width in pixels
% scnwcm     = 41.2; % screen width in cm
% view_dist  = 60;   % subject distance from screen in cm

%% SET VARIABLES

delay      = 200;  % To calculate Residual predictive gain: amount of time (ms)
                   % discounted from initial part of target path in occluder as
                   % eye velocity is maintained for this period

sacvel     =  35;  % Cut-off for saccade velocity (deg/s) - all velocities 
                   % above this are excluded from the averages
               
smvp       =   1;  % Degree of smoothing for velocity (EXCL sac) pre-averaging
smve       = 100;  % Degree of smoothing for velocity (EXCL sac) post-averaging
smvi       = 100;  % Degree of smoothing for velocity INCL saccades
                   % (derived from average displacement)
                   
omit       = [];   % trials to be omitted: AMENDED BELOW ALSO
               
%% SCRIPTS

importETdata     % Imports data in current folder
               % Adds NaN to missing parts & puts data in trialsummary struct
               % Centres and flips traces
               
if condition == 1
    
    FpC = trialsummary(1,1).speed.fpc;  % frames per cycle
     
    targoccplot1    % Creates target paths and occluder positions
                
    overlaytrials1  % Plots data from each individual trial, by group
                 
    metrics1        % Calculates errors and velocities (excluding saccades)
                    % Plots RMSE's for each trial  
                    
    disp('set outlier threshold')
    return
                  
    toomit1         % Allows threshold for outliers to be reset
                    % Determines which trials to omit from further analysis
            
    grandaverage1   % Ignores trials to be omitted from averages
                    % Calculates mean displacements for each group
                    % Calculates RMSE's for each group
                    % Calculates mean velocities (incl and excl saccades) 
                    % Smoothes velocities
                                           
    gains1          % Calculates gains
                            
    finalplots1     % Plots all graphs
    
else if condition == 2
        
        FpC = trialsummary(1,1).speed.fpc;  % frames per cycle
        
        targoccplot2
        overlaytrials2
        metrics2
        grandaverage2
        gains2
        finalplots2
        
    else  % (condition 3/4)
        
        FpC18 = [];
        FpC22 = [];
        fpc   = [];
        for t = 1:8  % find FpC data from 1st few files and allocate to FpC18/22
            try
                if trialsummary(1,t).speed.deg == 18
                    FpC18 = trialsummary(1,t).speed.fpc;
                end
                if trialsummary(1,t).speed.deg == 22
                    FpC22 = trialsummary(1,t).speed.fpc;
                end
            catch    % if .deg field doesn't exist
                fpc(1,t) = trialsummary(1,t).speed.fpc;
                if t == 8
                    FpC18 = max(fpc);
                    FpC22 = min(fpc);
                    if FpC18 == FpC22
                        disp('FpC error: only one FpC value found');
                    end
                end
            end
            if ~isempty(FpC18) == 1 && ~isempty(FpC22) == 1
                break
            end
        end
        
        targoccplot
        overlaytrials3
        metrics3
        grandaverage3
        gains3
        finalplots3
    
    end
end
                              